package com.smart.sso.server.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.model.Result;
import com.smart.mvc.model.ResultCode;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.sso.server.controller.common.BaseController;
import com.smart.sso.server.enums.TrueFalseEnum;
import com.smart.sso.server.model.RegisterUser;
import com.smart.sso.server.model.Role;
import com.smart.sso.server.model.User;
import com.smart.sso.server.provider.PasswordProvider;
import com.smart.sso.server.service.RegisterUserService;
import com.smart.sso.server.service.RoleService;
import com.smart.sso.server.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;

/**
 * @author YM
 */
@Api(tags = "用户注册")
@Controller
@RequestMapping("/register")
public class RegisterController extends BaseController {
	
	@Resource
	private UserService userService;
	
	@Resource
	private RegisterUserService registerUserService;
	
	@Resource
	private RoleService roleService;
	
	@ApiOperation("注册页")
	@RequestMapping(method = RequestMethod.GET)
	public String register(Model model) {
		List<Role> list = roleService.findByAll(TrueFalseEnum.TRUE.getValue() , null);
		model.addAttribute("roleList", list);
		return "register";
	}
	
	@ApiOperation("注册提交")
	@ApiResponse(response = Result.class, code = 200, message = "success")
	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody Result register(
			@ApiParam(value = "注册用户名", required = true) @ValidateParam({ Validator.NOT_BLANK }) String account,
			@ApiParam(value = "密码 ") String password,
			@ApiParam(value = "角色ids") String roleIds,
			@ApiParam(value = "用户留言") String message) {
		RegisterUser registerUser = new RegisterUser();
		registerUser.setAccount(account);
		registerUser.setPassword(PasswordProvider.encrypt(password));
		registerUser.setRoleIds(roleIds);
		List<Role> roleList = roleService.findByIds(getAjaxIds(roleIds));
		String roleNames = "";
		for(int i = 0; i<roleList.size()-1; i++)
		{
			roleNames = roleNames + roleList.get(i).getName() + ", ";
		}
		roleNames = roleNames + roleList.get(roleList.size()-1).getName();
		registerUser.setRoleNames(roleNames);
		registerUser.setRegisterTime(new Date());
		registerUser.setMessage(message);
		registerUserService.save(registerUser);
		
		return Result.createSuccessResult();
	}
	
	@ApiOperation("验证注册名")
	@RequestMapping(value = "/validateAccount", method = RequestMethod.POST)
	public @ResponseBody Result validateAccount(
			@ApiParam(value = "登录名", required = true) @ValidateParam({ Validator.NOT_BLANK }) String account) {
		Result result = Result.createSuccessResult();
		User user = userService.findByAccount(account);
		if (null != user) {
			result.setCode(ResultCode.ERROR).setMessage("注册的用户名已存在！");
		}
		RegisterUser registerUser = registerUserService.findByAccount(account);
		if (null != registerUser) {
			result.setCode(ResultCode.ERROR).setMessage("已有人使用该用户名提出申请！");
		}
		return result;
	}
	

}
